/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.dataprovider.view.wizard.panels.input;

import com.compomics.dataprovider.control.logic.fasta.UniprotFastaProvider;
import com.compomics.dataprovider.control.logic.taxonomy.UniprotTaxonomyProvider;
import com.compomics.dataprovider.view.taxonomy.TaxonomyTreeDialog;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class InputTaxonomyPanel extends javax.swing.JPanel {

    private static final Logger LOGGER = Logger.getLogger(InputTaxonomyPanel.class);
    private boolean skip = false;
    private ArrayList<String> selectedTaxonomies = new ArrayList<>();

    /**
     * Creates new form InputMethodPanel
     */
    public InputTaxonomyPanel() {
        initComponents();
        jList1.setModel(new DefaultListModel());
        rdbReviewOnly.setSelected(true);
    }

    public boolean isSkipped() {
        return skip;
    }

    public void setSkipped(boolean skip) {
        this.skip = skip;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        tfTaxonomyID = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        rdbReviewOnly = new javax.swing.JRadioButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("What will the taxonomyID(s) be for the creation of the FASTA-file ?");

        jScrollPane1.setBorder(null);

        jList1.setBorder(javax.swing.BorderFactory.createTitledBorder("Species"));
        jList1.setToolTipText("Press the - button to delete the selected values from this list. Hold down the CTRL-key for multiple selection.");
        jScrollPane1.setViewportView(jList1);

        tfTaxonomyID.setText("taxonomyID");
        tfTaxonomyID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfTaxonomyIDActionPerformed(evt);
            }
        });

        jButton1.setText("+");
        jButton1.setToolTipText("Add selected taxonomies to the processing list.");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("-");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/tree-icon24x24.png"))); // NOI18N
        jButton3.setToolTipText("Use the Taxonomy Browser to find UniProt taxonomy IDs");
        jButton3.setPreferredSize(new java.awt.Dimension(50, 50));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        rdbReviewOnly.setBackground(new java.awt.Color(255, 255, 255));
        rdbReviewOnly.setSelected(true);
        rdbReviewOnly.setText("Only Use Reviewed Sequences");
        rdbReviewOnly.setToolTipText("In case FASTA-file have to be downloaded, use this option to exclude all non-gold star proteins in UniProt.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdbReviewOnly)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfTaxonomyID, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 15, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tfTaxonomyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1)
                        .addComponent(jButton2))
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addComponent(rdbReviewOnly)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2, jButton3, tfTaxonomyID});

    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            String taxonomyID = tfTaxonomyID.getText();
            UniprotTaxonomyProvider provider = new UniprotTaxonomyProvider();
            ((DefaultListModel) jList1.getModel()).addElement(provider.queryTaxonomy(taxonomyID, true));
        } catch (IOException ex) {
            LOGGER.error(ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    public boolean isReviewOnly() {
        return rdbReviewOnly.isSelected();
    }

    private boolean validateTaxonomy(String taxonomyID) {
        return UniprotFastaProvider.exists(taxonomyID);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int[] selectedIndices = jList1.getSelectedIndices();
        for (int anIndex : selectedIndices) {
            ((DefaultListModel) jList1.getModel()).remove(anIndex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        TaxonomyTreeDialog dialog = new TaxonomyTreeDialog(null, true);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        for (String taxonomyID : dialog.getSelectedOrganisms()) {
            ((DefaultListModel) jList1.getModel()).addElement(taxonomyID);
            dialog.clearList();
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void tfTaxonomyIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfTaxonomyIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfTaxonomyIDActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rdbReviewOnly;
    private javax.swing.JTextField tfTaxonomyID;
    // End of variables declaration//GEN-END:variables

    public List<String> getSelectedTaxonomies() {
        DefaultListModel model = ((DefaultListModel) jList1.getModel());
        ArrayList<String> selectedTaxonomyIDsFromDialog = new ArrayList<>();
        for (int i = 0; i < model.size(); i++) {
            selectedTaxonomyIDsFromDialog.add(((String) model.getElementAt(i)).replace(" ", "+"));
        }
        return selectedTaxonomyIDsFromDialog;
    }

    public void reset() {
        ((DefaultListModel) jList1.getModel()).removeAllElements();
    }

}
